<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'xmlconverter.php';
  
  class PhotosController extends JController
  {  
    function display(){
    
      $document =& JFactory::getDocument();
      $view = & $this->getView($this->_name, $document->getType());
	  
	  $mseason     = $this->getModel('season', 'seasonmodel');
	  $season      = $mseason->get_latest();
      $weeks       = $this->get_weeks($season);      
      $week_number = JRequest::getVar('id');
      
	  if(!empty($weeks)){
	    if(!$week_number && $week_number!='0')
          $week_number = $this->get_current_week($weeks);
      
        $week_dates = $this->get_week_dates($season, $week_number);
	  
	    $mschedule = $this->getModel('schedule', 'schedulemodel');
	    $schedules = $mschedule->get_by_schedule($week_dates['start'], $week_dates['end']);
	  
	    $pics = $this->get_pics_by_schedules($schedules);
        $view->assign("pics", $pics);
	    $view->assign("weeks", $weeks);
	    $view->assign("current_week", $week_number);
	  }
      
      $view->display();
    }
	
	/* returns the weeks within the season indexed from 0.... */
    private function get_weeks($season){
      
      $result = array();
      if(!$season)
        return $result;
      
      $start  = strtotime($season->start_date);
      $end    = strtotime($season->end_date);
      $sec_wk = 60*60*24*7;
      $weeks  = floor(($end - $start)/$sec_wk);
      
      for($i = 0; $i < $weeks; $i++){
        $result[$i] = new stdClass();
        $result[$i]->start = strftime('%Y-%m-%d', $start + ($i * $sec_wk));
        $result[$i]->end   = strftime('%Y-%m-%d', $start + (($i + 1) * $sec_wk));
      }
      
      return $result;
    }
    
    /* returns the current week number*/
    private function get_current_week($weeks){
      
      $result = 0;
      $now    = time();
      foreach($weeks as $index => $week){
        $start = strtotime($week->start);
        $end   = strtotime($week->end);
        if($start <= $now && $end >= $now)
          return $index;
      }
      return $result;
    }
	
	private function get_week_dates($season, $week_number){
	
	  $result = array();
	  $start = strtotime($season->start_date);
	  $end   = strtotime($season->end_date);
	  $sec_wk = 60*60*24*7;
	  
	  $result['start'] = strftime('%Y-%m-%d', $start + ($week_number * $sec_wk));
	  $result['end']   = strftime('%Y-%m-%d', $start + (($week_number + 1) * $sec_wk));
	  return $result;
	}
	
	private function get_pics_by_schedules($schedules){
	
	  $mpic  = $this->getModel('pic', 'picmodel');
	  $mteam = $this->getModel('team', 'teammodel');
	  
	  $pics  = array();
	  foreach($schedules as $schedule){
	  
	    $first_team  = $mteam->get($schedule->first_team_id);
		$second_team = $mteam->get($schedule->second_team_id);
	  
	    $partial_pics = $mpic->get_pics_by_instance('GAMES', $schedule->id);
		if($partial_pics){
		  foreach($partial_pics as $p){
		    $p->title = $first_team->name.' Vs. '.$second_team->name;
		    array_push($pics, $p);
		  }
		  
	    }
		
	  }
	  return $pics;
	}
  }
?>